package com.platform.core.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.google.common.collect.Lists;
import com.platform.comm.dto.DtoMapper;
import com.platform.comm.service.impl.CommonBizImpl;
import com.platform.core.entity.CdProductManageEntity;
import com.platform.core.entity.QCdProductManageEntity;
import com.platform.core.repository.CdProductManageRepository;
import com.platform.core.service.CdProductManageBiz;
import com.platform.dto.CdProductInfoDto;
import com.platform.dto.CdProductManageDto;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Description: CdProductManage-业务接口实现类
 *
 * @author Coder
 */
@Slf4j
@Service
public class CdProductManageBizImpl extends CommonBizImpl<CdProductManageRepository, CdProductManageEntity, String> implements CdProductManageBiz {

    private final QCdProductManageEntity qCdProductManage = QCdProductManageEntity.cdProductManageEntity;
    private final DtoMapper mapper;

    public CdProductManageBizImpl(CdProductManageRepository cdProductManageRepository, DtoMapper mapper) {
        super(cdProductManageRepository);
        this.mapper = mapper;
    }

    @Override
    public Page<CdProductManageDto> search(Map<String,String> params, Pageable pageable){
        String comId = MapUtil.getStr(params, "comId");
        String category = MapUtil.getStr(params, "category");
        String color = MapUtil.getStr(params, "color");
        String productName = MapUtil.getStr(params, "productName");
        BooleanBuilder predicate = new BooleanBuilder();
        if (StringUtils.isNotEmpty(comId)){
            predicate.and(qCdProductManage.comId.eq(comId));
        }
        if (StringUtils.isNotEmpty(color)){
            predicate.and(qCdProductManage.color.eq(color));
        }
        if (StringUtils.isNotEmpty(category)){
            predicate.and(qCdProductManage.category.eq(category));
        }

        if (StringUtils.isNotEmpty(productName)){
            predicate.and(qCdProductManage.productName.eq(productName));
        }
        Page<CdProductManageEntity> page = queryByPage(predicate, pageable);
        return page.map(entity -> mapper.map(entity, CdProductManageDto.class));
    }

    @Override
    public CdProductManageDto detail(String id) {
        return mapper.map(fetchById(id), CdProductManageDto.class);
    }

    @Override
    public void createOne(CdProductManageDto req) {
        CdProductManageEntity entity = mapper.map(req, CdProductManageEntity.class);
        saveEntity(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(CdProductInfoDto req) {
        List<CdProductManageDto> productInfos = req.getProductInfos();
        String comId = req.getComId();
        List<CdProductManageEntity> objects = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(productInfos)){
            productInfos.forEach(item ->{
                CdProductManageEntity entity = mapper.map(item, CdProductManageEntity.class);
                entity.setComId(comId);
                objects.add(entity);
                repository.saveAll(objects);
            });
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(CdProductManageDto req) {
        CdProductManageEntity entity = fetchById(req.getId());
        mapper.map(req, entity);
        saveEntity(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void remove(String id){
        repository.deleteById(id);
    }
}
